// pages/performance/performance.js
import api from "../../api/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //#region 设置搜索框
    searchList: [
      {
        name: "shop_id",
        type: "topShop",
        offRefresh: true,
      },
      {
        name: "topDate",
        names: ["time_form", "start_date", "end_date"],
        type: "topDate",
      },
      {
        name: "search",
        type: "input",
        label: "会员姓名、手机号",
        span: 16,
      },
      {
        name: "coach_id",
        type: "pickerCoach",
        label: "教练",
        span: 8,
      },
      {
        name: "source",
        type: "picker",
        label: "来源",
        span: 7,
      },
     
      {
        name: "pay_status",
        type: "picker",
        label: "状态",
        span: 7,
      },
      {
        name: "finance",
        names: ['finance_form', 'junior_form'], 
        type: "multistepPicker",
        label: "类型",
        span: 10,
      },
      {
        type: "money",
        names: ["start_cost", "end_cost"],
        label: "金额",
        span: 16,
      },
    ],
    searchNum: [
      {
        label: "全部(元)",
        show: true,
        num: "0.00",
        subhead: "0笔"
      },
      {
        label: "会员卡(元)",
        num: "0.00",
        show: true,
        subhead: "0笔"
      },
      {
        label: "课程(元)",
        num: "0.00",
        show: true,
        subhead: "0笔"
      },
      {
        label: "周卡",
        num: "0.00",
        show: true,
        subhead: "0笔"
      },
      {
        label: "月卡",
        num: "0.00",
        show: true,
        subhead: "0笔"
      },
      {
        label: "季卡",
        num: "0.00",
        show: true,
        subhead: "0笔"
      },
      {
        label: "半年卡",
        num: "0.00",
        show: true,
        subhead: "0笔"
      },
      {
        label: "年卡",
        num: "0.00",
        show: true,
        subhead: "0笔"
      },
      {
        label: "其他卡",
        num: "0.00",
        show: true,
        subhead: "0笔"
      },
    ],
    sExplain: {
      title: "搜索说明",
      content: "时间可以选择，对会员名称、手机号、教练、来源、状态、类型、授课金额范围进行搜索",
    },

    nExplain: {
      title: "统计说明",
      content: "根据搜索条件对全部订单、会员卡订单、课程订单、周卡订单、月卡订单、季卡订单、半年卡订单、年卡订单、其他卡订单进行统计",
    },
    searchParam: {
      time_form: "month",
    },
    //#endregion
    payment: {
      app: {
        url: "/images/xcx.png",
        color: "#73F0EA",
      },
      qrcode: {
        url: "/images/skm.png",
        color: "#47CC88",
      },
      else: {
        color: "#EE8E3A",
        url: "/images/dhm.png",
      },
    },

    //#region 列表
    list: [],
    isMore: true,
    page: 1,
    isLoading: false,
    //#endregion
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      list: [],
      isMore: true,
      page: 1,
      "searchParam.shop_id": wx.getStorageSync("shopID"),
    });
    this.getDataList();
  },
  searchChonge(e) {
    this.setData({
      searchParam: e.detail,
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  getDataList() {
    let { page, list, searchParam, searchNum } = this.data;
    searchParam = {
      ...searchParam,
    };
    this.setData({
      isLoading: true,
    });
    api
      .apiStorePerList({
        ...searchParam,
        page,
      })
      .then((res) => {
        if (
          JSON.stringify(this.data.searchParam) !== JSON.stringify(searchParam)
        )
          return;
        let data = res.data;
        if (page === 1) {
          searchNum[0].num = Number(data.entirety.cost_sum).toFixed(2);
          searchNum[0].subhead = data.entirety.total_count + '笔';
          searchNum[1].num = Number(data.entirety.card_sum).toFixed(2);
          searchNum[1].subhead = data.entirety.card_count + '笔';
          searchNum[2].num = Number(data.entirety.course_sum).toFixed(2);
          searchNum[2].subhead = data.entirety.course_count + '笔';
          searchNum[3].num = Number(data.entirety.card_week_cost).toFixed(2);
          searchNum[3].subhead = data.entirety.card_week_count + '笔';
          searchNum[4].num = Number(data.entirety.card_month_cost).toFixed(2);
          searchNum[4].subhead = data.entirety.card_month_count + '笔';
          searchNum[5].num = Number(data.entirety.card_season_cost).toFixed(2);
          searchNum[5].subhead = data.entirety.card_season_count + '笔';
          searchNum[6].num = Number(data.entirety.card_half_cost).toFixed(2);
          searchNum[6].subhead = data.entirety.card_half_count + '笔';
          searchNum[7].num = Number(data.entirety.card_year_cost).toFixed(2);
          searchNum[7].subhead = data.entirety.card_year_count + '笔';
          searchNum[8].num = Number(data.entirety.card_other_cost).toFixed(2);
          searchNum[8].subhead = data.entirety.card_other_count + '笔';
          this.setData({
            searchNum,
          });
        }
        if (page >= data.all_page) this.setData({ isMore: false });
        if (data.list.length) {
          data.list = data.list.map((i) => {
            i.user_info.tagVal = 0;
            i.user_info.tagStr = "会员";
            return { ...i };
          });
          list.push(...data.list);
          this.setData({ list });
        }
      })
      .finally((_) => {
        this.setData({
          isLoading: false,
        });
      });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isMore) return;
    if (this.data.isLoading) return;
    let page = this.data.page;
    this.setData({
      page: page + 1,
    });
    this.getDataList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
